namespace Skymate.Commands
{
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;

    public class LightGroup
    {
        public const int BufferLength = 12;

        public LightGroup()
        {
            this.VehicleLane = new VehicleLane(VehicleLaneTypeEnum.Left, 1);
        }

        /// <summary>
        /// 灯组号
        /// </summary>
        [Category("信息"), Description("灯组号")]
        public byte GroupNo { get; set; }

        /// <summary>
        /// 方向
        /// </summary>
        [Category("信息"), Description("方向")]
        public DirectionEnum Direction { get; set; }

        /// <summary>
        /// 车道
        /// </summary>
        [Category("信息")]
        [DescriptionAttribute("展开查看车道选项")]
        public VehicleLane VehicleLane { get; set; }

        /// <summary>
        /// 灯类型
        /// </summary>
        [Category("信息"), Description("灯类型")]
        public LightTypeEnum LightType { get; set; }

        /// <summary>
        /// 绿灯端口号
        /// </summary>
        [Category("信息"), Description("绿灯端口号")]
        public byte GreenPortNumber { get; set; }

        /// <summary>
        /// 黄灯端口号
        /// </summary>
        [Category("信息"), Description("黄灯端口号")]
        public byte YellowPortNumber { get; set; }

        /// <summary>
        /// 红灯端口号
        /// </summary>
        [Category("信息"), Description("红灯端口号")]
        public byte RedPortNumber { get; set; }

        public static LightGroup Create(byte[] buffer)
        {
            var group = new LightGroup();
            group.GroupNo = buffer[0];
            group.Direction = (DirectionEnum)buffer[1];

            var vehicleLane = buffer[2];

            group.VehicleLane =
                VehicleLane.Create(vehicleLane);
            group.LightType = (LightTypeEnum)buffer[3];

            group.GreenPortNumber = buffer[4];
            group.YellowPortNumber = buffer[5];
            group.RedPortNumber = buffer[6];
            return group;
        }
        
        public string[] ToStrings()
        {
            IList<string> result = new List<string>();
            result.Add(this.GroupNo.ToString());
            result.Add(this.Direction.ToString());
            result.Add(this.VehicleLane.Type.ToString());
            result.Add(this.VehicleLane.LaneNumber.ToString());
            result.Add(this.LightType.ToString());

            result.Add(this.GreenPortNumber.ToString());
            result.Add(this.YellowPortNumber.ToString());
            result.Add(this.RedPortNumber.ToString());
            return result.ToArray();
        }

        public byte[] ToBytes()
        {
            byte[] buffer = new byte[12];
            buffer[0] = this.GroupNo;
            buffer[1] = (byte)this.Direction;

            buffer[2] = this.VehicleLane.ToByte();
            buffer[3] = (byte)this.LightType;

            buffer[4] = this.GreenPortNumber;
            buffer[5] = this.YellowPortNumber;
            buffer[6] = this.RedPortNumber;
            return buffer;
        }
    }
}